//
//  KLModelDetailViewController.m
//  Meboo
//
//  Created by kyo on 2023/12/27.
//

#import "KLModelDetailViewController.h"
#import "KLVipViewController.h"
#import "MBModelDetailHeaderView.h"
#import "MBModelDetailFooterView.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"
@interface KLModelDetailViewController ()
@property (nonatomic , strong) MBModelDetailHeaderView *headerView;
@property (nonatomic , strong) MBModelDetailFooterView *footerView;

@property (nonatomic , strong) UIScrollView *scrollView;
@property(nonatomic,strong) UILabel *titleLab;
@end

@implementation KLModelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXS(@"ffffff");
    [self setupUI];
    [self getRongCloudToken];
}

#pragma mark - Public Metohd
- (void)setModel:(MBWLMTopicModel *)model {
    _model = model;
}

- (void)setSubStr:(NSString *)subStr {
    _subStr = subStr;
    self.titleLab.text = subStr;
    [UILabel changeLineSpaceForLabel:self.titleLab WithSpace:MBHeightScale(8)];
}

- (void)getRongCloudToken {
    __WeakSelf
    [[MBChatDataManager sharedManager] getRongCloudToken:^(BOOL success) {
        if (success) {
            [weakSelf getRongCloudConnectState];
        }
    }];
}

- (void)getRongCloudConnectState {
//    __WeakSelf
    [[RCIM sharedRCIM] connectWithToken:[MBUserAccount defaultAccount].rongCloudToken
                               dbOpened:^(RCDBErrorCode code) {
        //消息数据库打开，可以进入到主页面
    } success:^(NSString *userId) {
        //连接成功,可跳转至会话列表页
        //存储本人的userid
        MBUserAccount  *account = [MBUserAccount defaultAccount];
        account.chatUserId = userId;
        [MBUserAccount  saveAccount:account];
    }error:^(RCConnectErrorCode status) {
        if (status == RC_CONN_TOKEN_INCORRECT) {
            //从 APP 服务获取新 token，并重连
            NSLog(@"");
        } else {
            //无法连接到 IM 服务器，请根据相应的错误码作出对应处理
            NSLog(@"");
        }
    }];
}

//vip是否还在有效期 yes:在有效期 no:过期了
- (BOOL)vipIsInDate {
    MBUserAccount *account = [MBUserAccount defaultAccount];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"]; // 设置日期格式
    
    NSDate *currentDate = [NSDate date];
    NSString *dateString = [formatter stringFromDate:currentDate];
    //    2020-10-11
    NSString *monthStr = [dateString substringFromIndex:5];
    monthStr = [monthStr substringToIndex:2];
    
    NSString *yearStr = [dateString substringToIndex:4];
    NSString *dayStr = [dateString substringFromIndex:8];
    
    NSInteger todayNum = [[NSString stringWithFormat:@"%@%@%@",yearStr,monthStr,dayStr] integerValue];
    NSInteger vipDateNum = [account.vipDateStr integerValue];
    
    if (vipDateNum >= todayNum) {
        return YES;
    } else {
        //过期了,更改存储信息,保存
        account.isVip = NO;
        [MBUserAccount saveAccount:account];
    }
    return NO;
}

#pragma mark - Action

- (void)vipNotice {
    [MBProgressHUD showInfoMessage:@"Please Become Vip First"];
    [self.navigationController pushViewController:[KLVipViewController new] animated:YES];
}

#pragma mark - UI
- (void)setupUI {
    [self.view addSubview:self.headerView];
    
    [self.view addSubview:self.footerView];
    [self.footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(MBHeightScale(120));
        make.bottom.mas_equalTo(MBHeightScale(-25)-TabbarSafeBottomMargin);
    }];

    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom).offset(MBHeightScale(20));
        make.bottom.equalTo(self.footerView.mas_top).offset(MBHeightScale(-20));
        make.left.right.equalTo(self.view);
    }];
    
    [self.scrollView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(MBWidthScale(20));
        make.width.mas_equalTo(ScreenWidth-MBWidthScale(40));
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - Setter && Getter

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.backgroundColor = HEXS(@"ffffff");
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.numberOfLines = 0;
        _titleLab.font = MBHelFontSizeScale(16);
        _titleLab.textColor = HEXS(@"333333");
    }
    return _titleLab;
}

- (MBModelDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[MBModelDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, MBHeightScale(208))];
        _headerView.model = self.model;
    }
    return _headerView;
}

- (MBModelDetailFooterView *)footerView {
    if (!_footerView) {
        _footerView = [MBModelDetailFooterView new];
        _footerView.model = self.model;
    }
    return _footerView;
}

@end
